﻿using System;
using System.IO;
using System.Xml.Serialization;

//UP主助手插件开发SDK

//请把生成的dll文件放在软件根目录下的“Addons”文件夹里面哦
//Properties/Assemblyinfo.cs是插件的信息，写过C#程序的都知道该如何写了！

namespace Demo//<--命名空间必须与你的文件名相同，比如：test.dll的命名空间必须为test
{
    public class plugin//<--本类名不能改，插件入口
    {

        #region UP主助手插件主事件。
        /// <summary>
        /// 软件主界面打开后触发start
        /// </summary>
        public void start()
        {

        }

        /// <summary>
        /// 软件关闭时触发stop
        /// </summary>
        public void stop()
        {

        }

        /// <summary>
        /// 弹幕来了触发dmshow
        /// </summary>
        public void dmshow(string danmuInfo)
        {
            DanmuInfo o = TransToEntity(danmuInfo);

        }

        /// <summary>
        /// 主程序会分享一些行为信息给插件，方便插件开发者知道主程序在干什么
        /// P.S.新的特性都会发送记录到这里
        /// </summary>
        public void ActionLog(string danmuInfo)
        {
            DanmuInfo o = TransToEntity(danmuInfo);

            Console.Write(o.logtext);
        }

        /// <summary>
        /// 在插件管理器双击插件会触发admin
        /// </summary>
        public void admin()
        {

        }
        #endregion

        #region 核心通讯
        public event EventHandler SMSG;
        private void SendMSG(string text, bool action = false)
        {
            string _text = "{dl_title}";
            if (action)
            {
                _text = "{action}";
            }
            SMSG(new object[] { _text + text }, null);
        }

        private DanmuInfo TransToEntity(string danmuInfo)
        {
            try
            {
                XmlSerializer xmls = new XmlSerializer(typeof(DanmuInfo));
                return (DanmuInfo)xmls.Deserialize(new StringReader(danmuInfo));
            }
            catch (Exception)
            {
                return null;
            }

        }
        #endregion
    }

    #region UP主助手实体类    
    [Serializable]
    public class DanmuInfo
    {
        public string logtext { get; set; }
        [Obsolete]
        public string uid { get; set; }
        public string bilibiliuid { get; set; }
        public string nickname { get; set; }
        public string datetime { get; set; }
        public int admin { get; set; }
        public int member { get; set; }
        public int vip { get; set; }
        public string roomid { get; set; }
        public string extra_info { get; set; }
        public string type { get; set; }
    }
    #endregion
}
